﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using BEGDC.PreContract.BLL;

namespace PreContractWeb.Administration
{
    public partial class EditDicItemDetail : System.Web.UI.Page
    {
        private BEGDC.PreContract.BLL.Authentication.PCIdentity user;

        protected void Page_Load(object sender, EventArgs e)
        {
            user = new BEGDC.PreContract.BLL.Authentication.PCIdentity(HttpContext.Current.User.Identity);
            Page.Header.Controls.Add(new LiteralControl(@"<base target=""_self"" />"));

            if (!ReportHelper.IsAdmin(user.EmployeeId))
            {
                this.divError.InnerHtml = AlertMessage.NoAdminPermission;
                this.divConnect.Visible = false;
                this.divError.Visible = true;
            }
            else if (!IsPostBack)
            {
                if (Request["id"] != null)
                {
                    BaseFunction.BindDicTypeDropDownList(dpDicItemType, string.Empty, true);
                    BaseFunction.BindDicItemDropDownListByDicType(dpProlemLV1, DicType.DictionaryType.ProblemLV1, string.Empty, false);
                    this.lblProlemLV2DicTypeID.Text = BaseFunction.CONST_PROBLEMLV2_DICTYPEID.ToString();

                    if (Request["id"] == "-1")
                    {
                        this.SaveButton.Text = "添加(Add)";
                        this.CNTitleLabel.Text = "添加字典条目";
                        this.ENTitleLabel.Text = "Add A DicItem";
                        this.ActiveCheckBox.Checked = true;
                        this.dpDicItemType.Items.Insert(0, new ListItem("", ""));
                        this.dpDicItemType.Items.RemoveAt(12);
                    }
                    else
                    {
                        this.SaveButton.Text = "保存(Save)";
                        this.CNTitleLabel.Text = "编辑字典条目";
                        this.ENTitleLabel.Text = "Edit DicItem Information";
                        this.LoadData();
                        this.dpDicItemType.Enabled = false;
                        this.dpProlemLV1.Enabled = false;
                        this.DicItemValueTextBox.Enabled = !(this.dpDicItemType.SelectedValue == BaseFunction.CONST_PRECONTRACT_COLUMNS_DICTYPE.ToString());
                    }
                }
                Response.Cache.SetCacheability(HttpCacheability.NoCache);
            }
        }

        protected void LoadData()
        {
            try
            {
                int dicItemID = int.Parse(Request["id"].ToString());

                BEGDC.PreContract.BLL.IDicItem iDicItem = (BEGDC.PreContract.BLL.IDicItem)BEGDC.Utility.Castle.AppContainer.Instance[BEGDC.PreContract.BLL.Services.CONST_DicItem_Service];

                BEGDC.PreContract.DAL.DicItem dicItem = iDicItem.GetDicItem(dicItemID);
                this.DicItemValueTextBox.Text = dicItem.DicItemValue;
                this.DicItemDescTextBox.Text = dicItem.DicItemDesc;
                this.ActiveCheckBox.Checked = dicItem.IsEnabled;

                ListItem item = dpDicItemType.Items.FindByValue(dicItem.DicTypeID.DicTypeID.ToString());
                if (item != null)
                {
                    this.dpDicItemType.SelectedIndex = dpDicItemType.Items.IndexOf(item);
                }

                if (this.dpDicItemType.SelectedValue == BaseFunction.CONST_PROBLEMLV2_DICTYPEID.ToString())
                {
                    this.trProblemLV1.Visible = true;

                    BEGDC.PreContract.BLL.IProblemLVAssignment iProblemLVAssignment = (BEGDC.PreContract.BLL.IProblemLVAssignment)BEGDC.Utility.Castle.AppContainer.Instance[BEGDC.PreContract.BLL.Services.CONST_ProblemLVAssignment_Service];
                    int problemLV1 = iProblemLVAssignment.GetProblemLV1IDByProblemLV2ID(dicItemID);

                    ListItem ItemLv1 = this.dpProlemLV1.Items.FindByValue(problemLV1.ToString());
                    this.dpProlemLV1.SelectedIndex = ItemLv1 != null ? dpProlemLV1.Items.IndexOf(ItemLv1) : 0;
                }
            }
            catch (Exception ex)
            {
                this.ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('" + ex.Message.Replace("'", "\\'") + "')</script>");
            }
        }

        private bool CheckValues()
        {
            if (string.IsNullOrEmpty(this.DicItemValueTextBox.Text.Trim()))
            {
                this.ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('请输入字典条目的名称！')</script>");
                return false;
            }
            if (string.IsNullOrEmpty(this.dpDicItemType.SelectedValue))
            {
                this.ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('请选择字典条目类型！')</script>");
                return false;
            }
            if (this.dpDicItemType.SelectedValue == BaseFunction.CONST_PROBLEMLV2_DICTYPEID.ToString() && this.dpProlemLV1.SelectedItem == null)
            {
                this.ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('请选择关联1级问题！')</script>");
                return false;
            }
            return true;
        }

        protected void SaveButton_Click(object sender, EventArgs e)
        {
            try
            {
                if (CheckValues())
                {
                    BEGDC.PreContract.BLL.IDicItem idicItem = (BEGDC.PreContract.BLL.IDicItem)BEGDC.Utility.Castle.AppContainer.Instance[BEGDC.PreContract.BLL.Services.CONST_DicItem_Service];
                    BEGDC.PreContract.BLL.IDicType idicItemType = (BEGDC.PreContract.BLL.IDicType)BEGDC.Utility.Castle.AppContainer.Instance[BEGDC.PreContract.BLL.Services.CONST_DicType_Service];

                    int dicItemID = int.Parse(Request["id"].ToString());
                    bool flag = true;

                    if (dicItemID == -1)
                    {
                        int numflag = idicItem.AddDicItem(idicItemType.GetDicType(int.Parse(this.dpDicItemType.SelectedValue)), this.DicItemValueTextBox.Text.Trim(), this.DicItemDescTextBox.Text.Trim(), this.ActiveCheckBox.Checked);
                        if (numflag == 0) flag = false;

                        if (numflag > 0 && this.dpDicItemType.SelectedValue == BaseFunction.CONST_PROBLEMLV2_DICTYPEID.ToString() && this.dpProlemLV1.SelectedItem == null)
                        {
                            BEGDC.PreContract.BLL.IProblemLVAssignment iProblemLVAssignment = (BEGDC.PreContract.BLL.IProblemLVAssignment)BEGDC.Utility.Castle.AppContainer.Instance[BEGDC.PreContract.BLL.Services.CONST_ProblemLVAssignment_Service];
                            BEGDC.PreContract.DAL.DicItem problemLV1 = new BEGDC.PreContract.DAL.DicItem();
                            problemLV1.DicItemID = Convert.ToInt32(this.dpProlemLV1.SelectedValue);
                            BEGDC.PreContract.DAL.DicItem problemLV2 = new BEGDC.PreContract.DAL.DicItem();
                            problemLV2.DicItemID = numflag;
                            iProblemLVAssignment.AddProblemLVAssignment(problemLV1, problemLV2);
                        }
                    }
                    else
                    {
                        BEGDC.PreContract.DAL.DicItem dicItem = idicItem.GetDicItem(dicItemID);
                        flag = idicItem.UpdateDicItem(dicItemID, idicItemType.GetDicType(int.Parse(this.dpDicItemType.SelectedValue)), this.DicItemValueTextBox.Text.Trim(), this.DicItemDescTextBox.Text.Trim(), this.ActiveCheckBox.Checked);
                    }
                    if (!flag)
                    {
                        this.ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('" + AlertMessage.ExistedRecord + "')</script>");
                        return;
                    }
                    this.Response.Write("<script language='javascript'>alert('" + BaseFunction.ChangeAlertMessage(AlertMessage.OperationSuccess) + "');var arr=new Array(); arr[0]=0; window.returnValue=arr;window.close();</script>");
                }
            }
            catch (Exception ex)
            {
                this.ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('" + ex.Message.Replace("'", "\\'") + "')</script>");
            }
        }
    }
}